﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using Advisor.Entities;
using System.Collections;

namespace Advisor.Entities
{
  public class HistoryDAO
  {
      private Users m_Users;
    public HistoryDAO(Users user)
    {
        this.m_Users = user;
    }

    /// <summary>
    /// Retorna todas os registros da tabela History . 
    /// </summary>
    /// <param name="column"></param>
    /// <param name="maximumRows"></param>
    /// <param name="startRowIndex"></param>
    /// <returns></returns>
    internal IList GetHistories(string column, int maximumRows, int startRowIndex)
    {
      ISession session = NHibernateSession.GetSession();
      string sql = "FROM History as history";

      //Ordenação
      if (!String.IsNullOrEmpty(column))
      {
        sql += " ORDER BY history." + column;
      }
      IQuery query = session.CreateQuery(sql);

      //Paginando
      query.SetFirstResult(startRowIndex);
      query.SetMaxResults(maximumRows);

      return query.List();
    }
    /// <summary>
    ///  Método para inserir um Histórico no banco. 
    /// </summary>
    /// <param name="history"></param> 

    internal void InsertHistory(History history)

    {
      ISession session = NHibernateSession.GetSession();
      session.Save(history);
      session.Close();
    }
    /// <summary>
    /// Retorna uma instância da tabela histórico passando o id;
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>

    internal History GetHistory(int id)

    {
      ISession session = NHibernateSession.GetSession();
      return (History)session.Get(typeof(History), id);
    }


    /// <summary>
    /// Remove um Histórico do banco
    /// </summary>
    /// <param name="history"></param>
    internal void DeleteHistory(History history)
    {
      ISession sessions = NHibernateSession.GetSession();
      sessions.Delete(history);
      sessions.Close();
    }

    /// <summary>
    /// Atualiza um Histórico do banco. 
    /// </summary>
    /// <param name="history"></param>
    internal void UpdateHistory(History history)
    {
      ISession session = NHibernateSession.GetSession();
      session.Update(history);
      //Comando que ativa as alterações no banco de dados. 
      session.Flush();
      session.Close();
    }

  }
}
